#include "regform.h"
#include "ui_regform.h"

regForm::regForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::regForm),
    manager(new QNetworkAccessManager(this))

{
    ui->setupUi(this);
}

regForm::~regForm()
{
    delete ui;
}

void regForm::on_pushButton_clicked()
{
    filePath = QFileDialog::getOpenFileName(this, "选择新员工照片", "C:", "*.jpg *.png");

    QPixmap map(filePath);
    //    调整图片大小跟标签适应
    map.scaled(ui->label->width(),ui->label->height());
    //    让你的配置生效
    ui->label->setScaledContents(true);
    ui->label->setPixmap(map);
}

void regForm::on_pushButton_3_clicked() {
    QString userid = ui->lineEdit->text();
    QString name = ui->lineEdit_2->text();
    QString department = ui->lineEdit_3->text();
    QString position = ui->lineEdit_4->text();

    // 前端校验
    if (userid.isEmpty() || name.isEmpty() || filePath.isEmpty()) {
        QMessageBox::warning(this, "提示", "员工号、姓名和照片不能为空");
        return;
    }

    // 准备请求数据
    QMap<QString, QString> formData;
    formData["employeeId"] = userid;
    formData["name"] = name;
    formData["department"] = department;
    formData["position"] = position;

    // 调用封装的网络框架
    HttpManager::instance()->postWithFile(
                QString(SERVER_BASE_URL) + "/api/employee/register",
                formData,
                "photo",  // 文件表单名
                filePath,
                // 成功回调（只关心业务逻辑）
                [this](const QJsonObject& result) {
        if (result["success"].toBool()) {
            QMessageBox::information(this, "成功", "注册成功");
        } else {
            QMessageBox::critical(this, "失败", result["error"].toString());
        }
    },
    // 错误回调（统一处理所有错误类型）
    [this](int code, const QString& msg) {
        QMessageBox::critical(this, "错误", QString("代码: %1, 原因: %2").arg(code).arg(msg));
    }
    );
}
